package org.example.tms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.tms.config.AjaxResult;
import org.example.tms.mapper.EmpMapper;
import org.example.tms.pojo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpMapper empManager;
//    请求路径：/login
//    请求方式：POST
//    接口描述：该接口用于员工登录Tlias智能学习辅助系统，登录完毕后，系统下发JWT令牌。
//参数：{"username": "jinyong", "password": "123456"}
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Emp emp){
        log.info("员工登录: {}", emp);
        //1.构建查询条件
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", emp.getUsername());
        queryWrapper.eq("password", emp.getPassword());
        Emp loginEmp = empManager.selectOne(queryWrapper);
        if (loginEmp == null) {
            return AjaxResult.warn("用户名或密码错误", null);
        }
        //这里不定义单独的返回类，使用Map，具体字段参考登录接口描述
        HashMap<Object, Object> resultMap =  new HashMap<>();
        resultMap.put("username", loginEmp.getUsername());
        resultMap.put("name", loginEmp.getName());
        resultMap.put("id", loginEmp.getId());
        //这里搞个假的令牌，解决登录的问题
        resultMap.put("token", "WO shi jia de   token");

        return AjaxResult.success(resultMap);
    }

}